package springboot.rabbitmq.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springboot.rabbitmq.config.ConfirmConfig;

@Slf4j
@RestController
@RequestMapping("/confirm")
public class ProducerController {
    
    @Autowired
    private RabbitTemplate rabbitTemplate;
    
    @GetMapping("/sendMessage/{message}")
    public void sendMessage(@PathVariable("message") String message){

        CorrelationData correlationData1 = new CorrelationData("1");
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME, ConfirmConfig.ROUTING_KEY,message+"key1",correlationData1);
        log.info("发送消息内容为：{}",message+"key1");

        // 测试交换机出问题了(故意把交换机名称写错，模拟交换机宕机的场景)
        //CorrelationData correlationData2 = new CorrelationData("2");
        //rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME+"xxx", ConfirmConfig.ROUTING_KEY,message,correlationData2);
        //log.info("发送消息内容为：{}",message+"key1");

        // 测试交换机没问题时，队列出问题的场景(故意把routing_key写错，模拟队列出现了问题)
        CorrelationData correlationData3 = new CorrelationData("3");
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME, ConfirmConfig.ROUTING_KEY+"yyy",message+"key1yyy",correlationData3);
        log.info("发送消息内容为：{}",message+"key1yyy");
    }
}
